package cn.sirun.com.fca.account.presenter.impl;

import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.constant.AccountActionConst;
import cn.sirun.com.fca.account.loader.InputOldPinFlowLoader;
import cn.sirun.com.fca.account.model.bind.PinSetParamResponse;
import cn.sirun.com.fca.account.model.bind.PinSetParamsRequest;
import cn.sirun.com.fca.account.model.bind.VerifyPinRequest;
import cn.sirun.com.fca.account.model.bind.VerifyPinResponse;
import cn.sirun.com.fca.account.presenter.AccountFlowPresenter;
import cn.sirun.com.fca.account.presenter.IInputOldPinPresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class InputOldPinPresenter extends AccountFlowPresenter<InputOldPinFlowLoader> implements
        IInputOldPinPresenter {
    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    protected InputOldPinFlowLoader createLoader() {
        return new InputOldPinFlowLoader();
    }


    @Override
    public void getUpdatePinSetParams(PinSetParamsRequest request) {
        getLoader().getUpdatePinSetParams(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<PinSetParamResponse>>() {

                    @Override
                    public void onNext(BaseResponse<PinSetParamResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        InputOldPinPresenter.this.getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_UPDATE_PIN_SET_PARAMS_RESULT,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    public void verifyPin(VerifyPinRequest request) {
        getLoader().verifyPin(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<VerifyPinResponse>>() {

                    @Override
                    public void onNext(BaseResponse<VerifyPinResponse> submitPhoneResponse) {
                        super.onNext(submitPhoneResponse);
                        InputOldPinPresenter.this.getActionListener().onAction
                                (AccountActionConst.Normal.ACTION_VERIFY_PIN_RESULT,
                                        submitPhoneResponse);
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }

}
